package com.aistar.controller;


import com.aistar.pojo.Member;
import com.aistar.service.RegistService;
import com.aistar.util.Config;
import com.aistar.util.JsonResult;

import com.aistar.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class RegistController {
    @Autowired
    private RegistService registService;

    //注册验证码请求
    @PostMapping("regist/getcode")
    @ResponseBody
    public JsonResult getRegistCode(Long userTel){
        JsonResult jsonResult = registService.selectByPhone(userTel);
        if(jsonResult.getCode() ==0){//用户存在 提醒用户登录
            return jsonResult;
        }else {
            System.out.println(userTel); //用户不存在 可以注册 发送验证码
            //类型转换
            String userTel2 = String.valueOf(userTel);
            String codeMsg = Config.getCode(userTel2);
            System.out.println(codeMsg);
            return new JsonResult(1,"用户不存在可以注册", codeMsg);

        }
    }

    //注册

    @PostMapping("regist/noist")
    @ResponseBody
    public JsonResult getRegist(Member member,HttpServletRequest request){

        System.out.println("请求页面成功");
        String userTel = request.getParameter("userTel");
        String userPassword = MD5Utils.md5(request.getParameter("userPassword"));
        String userNickname = request.getParameter("userNickname");
        String userName = userNickname;
        String userAccount = userTel;
        member.setUserAccount(userAccount);
        member.setUserNickname(userNickname);
        //类型转换
        Long userTel2 = Long.parseLong(userTel);
        member.setUserTel(userTel2);
        member.setUserPassword(userPassword);
        member.setUserName(userName);
        member.setUserStatus(1);
        member.setUserPicture("images/wKgED1uqImOAAxCCAAAeJTVWYJU680.png");
        System.out.println(member);
        boolean flag = registService.save(member);
        if(flag){
            return new JsonResult(0,"注册成功",member);
        }
        return new JsonResult(1,"注册失败",null);
    }

}
